import 'package:flutter/material.dart';
import 'package:hktk_app/dao/my_dao.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/model/package/package_model.dart';
import 'package:hktk_app/painter/line_painter.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/page_title_widget.dart';

/// 激活页面
class ActivationPage extends StatefulWidget {
  const ActivationPage({super.key});

  @override
  State<ActivationPage> createState() => _ActivationPageState();
}

class _ActivationPageState extends State<ActivationPage> {
  late BuildContext _context;

  /// 输入框控制器
  late TextEditingController _textEditingController;

  /// 激活套餐的标题显示
  String? _packageTitle;

  /// 题库科目
  String? _subject;

  @override
  void initState() {
    super.initState();
    debugPrint("activationPage 页面 -> initState 方法执行 ===> ");
    debugPrint("");
    _packageTitle = null;
    _subject = null;
    _textEditingController = TextEditingController();
  }

  @override
  void dispose() {
    super.dispose();
    debugPrint("activationPage 页面 -> dispose 方法执行 ===> ");
    debugPrint("");
    _textEditingController.dispose();
    ToastUtil.loadingBoxRemove();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("activationPage 页面 -> build 方法执行 ===> ");
    debugPrint("");
    _context = context;
    return Scaffold(
      backgroundColor: colorWhite2,
      body: Column(
        children: [
          PageTitleWidget(name: "激活题库", pageContext: context),
          Expanded(child: _content()),
        ],
      ),
    );
  }

  /// 内容显示
  _content() {
    double bottom = MediaQuery.of(_context).padding.bottom;
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 24),
      child: MediaQuery.removePadding(
        removeTop: true,
        context: _context,
        child: ListView(
          children: [
            hiSpace(height: 24),
            _showItems(),
            hiSpace(height: 80),
            _btnItem(),
            hiSpace(height: 24 + bottom),
          ],
        ),
      ),
    );
  }

  /// 按钮组件
  _btnItem() {
    return GestureDetector(
      onTap: _onTapFunJh,
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 8),
        margin: EdgeInsets.symmetric(horizontal: 6),
        decoration: BoxDecoration(
          color: colorBlue1,
          borderRadius: BorderRadius.circular(49),
        ),
        child: Center(
          child: Text(
            "激活",
            style: FontUtil.fontWhile1(24),
          ),
        ),
      ),
    );
  }

  /// 内容显示
  _showItems() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20),
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.all(Radius.circular(20)),
      ),
      child: Column(
        children: [
          _codeItem(),
          _packageItem(),
          _subjectItem(),
        ],
      ),
    );
  }

  /// item显示
  _showItem({
    required String title,
    required Widget child,
    bool showLine = true,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 662,
          height: 90,
          child: Row(
            children: [
              SizedBox(
                width: 152,
                child: Text(
                  title,
                  style: FontUtil.fontBlack1(20),
                ),
              ),
              Expanded(child: child),
            ],
          ),
        ),
        if (showLine)
          CustomPaint(
            painter: LinePainter(
              color: colorGrey14,
              length: 662,
              isDashed: true,
              lineWidth: 1,
            ),
          )
      ],
    );
  }

  /// 套装显示
  _packageItem() {
    return _showItem(
      title: "激活套餐",
      child: Text(
        _packageTitle ?? "",
        style: FontUtil.fontBlue1(24),
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }

  /// 科目显示
  _subjectItem() {
    return _showItem(
      title: "题库科目",
      child: Text(
        _subject ?? "",
        style: FontUtil.fontBlue1(24),
        // maxLines: 1,
        // overflow: TextOverflow.ellipsis,
      ),
    );
  }

  /// 激活码组件
  _codeItem() {
    return _showItem(
      title: "激活码",
      child: SizedBox(
        height: 40,
        child: TextField(
          controller: _textEditingController,
          // 设置光标颜色
          cursorColor: colorBlue1,
          // 光标高度
          cursorHeight: 42,
          // 样式
          style: FontUtil.fontBlue1(24),
          // 装饰
          decoration: InputDecoration(
            // 去掉textField自定的高度
            isCollapsed: true,
            // 内容边距
            contentPadding: EdgeInsets.only(bottom: 3),
            // 去除边框
            border: InputBorder.none,
          ),
        ),
      ),
    );
  }

  /// 激活按钮点击，你点击了激活按钮
  _onTapFunJh() {
    var text = _textEditingController.text;
    debugPrint("activationPage 页面 -> 你点击了激活按钮 ===> $text");
    // 开启加载框
    ToastUtil.loadingBoxShow(_context);
    MyDao.openSkuCode(code: text).then((SkuModel sku) {
      // 加载完成
      ToastUtil.loadingBoxRemove();
      ToastUtil.show(context: _context, msg: "激活成功");
      setState(() {
        _packageTitle = sku.packageName ?? "";
        _subject = sku.categoryNameList?.join(",") ?? "";
      });
    }).catchError((e) {
      ToastUtil.loadingBoxRemove();
      debugPrint("activationPage 页面 -> 激活码激活发生异常 ===> $e");
      String msg = "未知错误，请重试";
      if (e is DaoException) {
        debugPrint("activationPage 页面 -> dao异常 ===> ${e.msg}");
        msg = e.msg;
      }
      ToastUtil.show(context: _context, msg: msg);
    });
  }
}
